package service

import (
	"context"
	"go-pan/internal/model"
	"go-pan/internal/repository"
)

type FileMetaService interface {
	Create(ctx context.Context, meta *model.FileMetadata) error
	GetByID(ctx context.Context, id int64) (*model.FileMetadata, error)
	MarkDeleted(ctx context.Context, id int64) error
	HardDelete(ctx context.Context, id int64) error
	Restore(ctx context.Context, id int64) error
	ListFilesByParent(ctx context.Context, ownerID int64, parentID int64) ([]*model.FileMetadata, error)
	ListDeletedFiles(ctx context.Context, ownerID int64) ([]*model.FileMetadata, error)
	FindByHashAndSize(ctx context.Context, hash string, size int64) (*model.FileMetadata, error)
	HasChildren(ctx context.Context, ownerID int64, parentID int64) (bool, error)
	FindByUniqueKey(ctx context.Context, ownerID int64, parentID int64, nameKey string) (*model.FileMetadata, error)
	Move(ctx context.Context, fileID int64, newParentID int64) error
	Rename(ctx context.Context, id int64, newName, newKey string) error
	GetOldDeletedFiles(ctx context.Context, ownerID int64, daysOld int) ([]*model.FileMetadata, error)
	GetAllDeletedFilesOverDays(ctx context.Context, daysOld int) ([]*model.FileMetadata, error)
}

type fileMetaService struct {
	repo repository.FileMetaRepository
}

func NewFileMetaService(repo repository.FileMetaRepository) FileMetaService {
	return &fileMetaService{repo: repo}
}

func (s *fileMetaService) Create(ctx context.Context, meta *model.FileMetadata) error {
	return s.repo.Create(ctx, meta)
}

func (s *fileMetaService) GetByID(ctx context.Context, id int64) (*model.FileMetadata, error) {
	return s.repo.FindByID(ctx, id)
}

func (s *fileMetaService) MarkDeleted(ctx context.Context, id int64) error {
	return s.repo.Delete(ctx, id)
}

func (s *fileMetaService) ListFilesByParent(ctx context.Context, ownerID int64, parentID int64) ([]*model.FileMetadata, error) {
	return s.repo.FindByParent(ctx, ownerID, parentID)
}

func (s *fileMetaService) FindByHashAndSize(ctx context.Context, hash string, size int64) (*model.FileMetadata, error) {
	return s.repo.FindByHashAndSize(ctx, hash, size)
}

func (s *fileMetaService) HasChildren(ctx context.Context, ownerID int64, parentID int64) (bool, error) {
	return s.repo.HasChildren(ctx, ownerID, parentID)
}

func (s *fileMetaService) Rename(ctx context.Context, id int64, newName, newKey string) error {
	return s.repo.Rename(ctx, id, newName, newKey)
}

func (s *fileMetaService) HardDelete(ctx context.Context, id int64) error {
	return s.repo.HardDelete(ctx, id)
}

func (s *fileMetaService) Restore(ctx context.Context, id int64) error {
	return s.repo.Restore(ctx, id)
}

func (s *fileMetaService) ListDeletedFiles(ctx context.Context, ownerID int64) ([]*model.FileMetadata, error) {
	return s.repo.ListDeletedFiles(ctx, ownerID)
}

func (s *fileMetaService) GetOldDeletedFiles(ctx context.Context, ownerID int64, daysOld int) ([]*model.FileMetadata, error) {
	return s.repo.FindDeletedFiles(ctx, ownerID, daysOld)
}

func (s *fileMetaService) GetAllDeletedFilesOverDays(ctx context.Context, daysOld int) ([]*model.FileMetadata, error) {
	return s.repo.FindAllDeletedFilesOverDays(ctx, daysOld)
}

func (s *fileMetaService) FindByUniqueKey(ctx context.Context, ownerID int64, parentID int64, nameKey string) (*model.FileMetadata, error) {
	return s.repo.FindByUniqueKey(ctx, ownerID, parentID, nameKey)
}

func (s *fileMetaService) Move(ctx context.Context, fileID int64, newParentID int64) error {
	return s.repo.Move(ctx, fileID, newParentID)
}
